odoo.define('nd_common.lot_field', function (require) {
    'use strict'
    var relational_fields = require('web.relational_fields');
    var FieldMany2One = relational_fields.FieldMany2One;
    var field_registry = require('web.field_registry');

    var LotField = FieldMany2One.extend({
        events: _.extend({}, FieldMany2One.prototype.events, {
            'click': '_onClick',
        }),
        _onClick: function (event) {
            var self = this;
            var relation = self.field.relation
            var mode = self.mode
            if (relation === 'stock.production.lot' && mode === 'readonly') {
                event.preventDefault();
                event.stopPropagation();
                this._rpc({
                    model: this.field.relation,
                    method: 'get_lot_move_line',
                    args: [[this.value.res_id]],
                    context: this.record.getContext(this.recordParams),
                })
                .then(function (action) {
                    self.trigger_up('do_action', {action: action});
                });
            } else {
                this._super.apply(this, arguments);
            }
        },

    });
    field_registry.add('LotField', LotField)
    return LotField
})